package strcy.cn.market.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import strcy.cn.market.pojo.ShoppingCarts;
import strcy.cn.market.pojo.VO.ShoppingCartsVO;
import strcy.cn.market.service.ShoppingCartsService;
import strcy.cn.market.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class ShoppingCartsController {
    @Autowired
    private ShoppingCartsService shoppingCartsService;

    //加入购物车
    @PostMapping("/add")
    public Result addCart(@RequestBody ShoppingCarts shoppingCarts) {
        shoppingCartsService.add(shoppingCarts);
        return Result.success();
    }
    //修改数量
    @PutMapping("/update")
    public Result updateCart(Integer quantity,Integer cartId) {

        shoppingCartsService.update(quantity,cartId);
        return Result.success();
    }
    //获取购物车列表
    @GetMapping("/list")
    public Result<List<ShoppingCartsVO>> list(){
        List<ShoppingCartsVO> list = shoppingCartsService.list();
        return Result.success(list);
    }

    //删除购物车
    @DeleteMapping("{cartId}")
    public Result deleteCart(@PathVariable("cartId") Integer cartId) {
        shoppingCartsService.deleteById(cartId);
        return Result.success();
    }

    //根据ID集合批量删除购物车
    @DeleteMapping("/delete")
    public Result deleteByCartIds(@RequestBody List<Integer> cartIds) {
        shoppingCartsService.deleteByIds(cartIds);
        return Result.success();
    }
}
